<template>
    <div class="login">
        <div class="loginbox">
            <p>
                千锋2213后台管理系统
            </p>
            <el-form ref="ruleFormRef" :model="ruleForm" status-icon :rules="rules" label-width="60px"
                class="demo-ruleForm">
                <el-form-item label="账号" prop="adminname">
                    <el-input v-model="ruleForm.adminname" type="text" autocomplete="off" />
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="ruleForm.password" type="password" autocomplete="off" />
                </el-form-item>
                <el-form-item>
                    <el-button size="large" type="primary" @click="submitForm(ruleFormRef)">登录</el-button>
                    <el-button size="large" @click="resetForm(ruleFormRef)">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
// 导入登录接口
import { loginApi } from '@/api/http'
console.log(2, loginApi);
export default {
    name: '',
    data() {
        return {
            ruleFormRef: null,
            ruleForm: { // 双向绑定的表单数据
                adminname: '',
                password: '',
            },
            rules: {
                adminname: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
                password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
            }
        };
    },
    methods: {
        // 提交表单
        submitForm(formEl) {
            if (!formEl) return
            formEl.validate((valid) => {
                if (valid) {
                    console.log('submit!')
                    console.log(this.ruleForm);
                    // 调用登录接口
                    loginApi(this.ruleForm).then(res => {
                        console.log(res);
                        if (res.data.code == 200) {
                            this.$router.push('/home')
                        }
                    })

                } else {
                    console.log('error submit!')
                    return false
                }
            })
        },
        // 重置表单
        resetForm(formEl) {
            if (!formEl) return
            formEl.resetFields()
        }
    },
    mounted() {
        console.log(this.$refs.ruleFormRef);// 获取表单实力对象
        this.ruleFormRef = this.$refs.ruleFormRef
    }


}

</script>
<style lang="scss" scoped>
* {
    margin: 0;
    padding: 0;
}

.login {
    width: 100%;
    height: 100%;
    background-image: url('@/assets/img/bg.jpeg');
    background-size: 100% 100%;
    display: flex;
    justify-content: center;
    align-items: center;

    .loginbox {
        width: 400px;
        height: 300px;
        background-color: white;

        >p {
            width: 100%;
            height: 60px;
            background-color: lightgreen;
            line-height: 60px;
            text-align: center;
            font-size: 25px;
            font-weight: 600;
            color: white;
        }
    }

    .el-form {
        padding: 30px;
        box-sizing: border-box;

    }

    .el-form-item {
        margin-bottom: 24px;
    }

    .el-button {
        width: 80px;
    }
}
</style>